package com.safe.first.common.constants;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.safe.first.common.exception.BizException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Optional;

/**
 * @author Mickey
 * @since 2021/10/25
 */
@Getter
@AllArgsConstructor
public enum CodeEnum implements BaseEnum<CodeEnum> {
    SUCCESS(1, "操作成功"),
    FAIL(0, "操作失败"),
    ;

    private final Integer code;
    private final String name;

    public static CodeEnum of(Integer code) {
        return Optional.ofNullable(BaseEnum.parseByCode(CodeEnum.class, code)).orElseThrow(() -> new BizException(CodeEnum.FAIL));
    }

    @JsonCreator
    public static CodeEnum create(EnumDict type) {
        return of(type.getCode());
    }
}
